#ifndef _PARTHUS_UT_ATS_IUT_H
#define _PARTHUS_UT_ATS_IUT_H
/*************************************************************************
 * MODULE NAME:    ut_ats_iut.h
 * PROJECT CODE:    pimiento
 * DESCRIPTION:    Implementation under test for the abstract test suite
 * MAINTAINER:     John Nelson
 * DATE:           21 Mar 2001
 *
 * SOURCE CONTROL: $Id: ut_ats_iut.h,v 1.1 2008/02/03 09:39:25 james Exp $
 *
 * LICENSE:
 *     This source code is copyright (c) 2001-2004 Ceva Inc.
 *     All rights reserved.
 *
 * REVISION HISTORY:
 *    V1.0     01 Feb  2001 -   jn     - Initial Version
 *
 * SOURCE:
 *    A set of convenient functions for controlling the unit under test
 *    and for accessing common functions.
 * ISSUES:
 *
 *************************************************************************/
#include "lc_interface.h"
#include "lmp_con_filter.h"
#include "lmp_acl_container.h"
#include "hc_flow_control.h"
#include "bt_test.h"
#include "test_support.h"


/*****************************************************************************
 * Allow flexible assignment of HCI queues, default is normal COMMAND/EVENT.
 *****************************************************************************/
void UTiut_Assign_HCI_Queues(u_int8 command_q, u_int8 event_q);
u_int8 UTiut_Get_HCI_Event_Q(void);
u_int8 UTiut_Get_HCI_Command_Q(void);


/*****************************************************************************
 * UTiut_Initialise
 *
 * Initialise the complete device and LC.
 *
 * p_bd_addr_iut        NULL                    Use default address
 *                      !NULL                   Use presented bd_addr
 *****************************************************************************/
/*
 * Default Class of Device for sending/verification
 */
#define UTiut_DEFAULT_CLASS_OF_DEVICE 0x001F00

void UTiut_Initialise_IUT(const t_bd_addr *p_bd_addr_iut);

/*****************************************************************************
 * Slot/Frame Processing
 *****************************************************************************/
void UTiut_Verify_No_Packet_Received_In_Rx_Slot(void);
void UTiut_Verify_No_Packet_Transmitted_In_Tx_Slot(void);

t_packet UTiut_Process_Slave_Rx_Tx_Frame(void);
t_packet UTiut_Process_Master_Tx_Rx_Frame(void);
t_packet UTiut_Complete_Current_Frame(u_int8 role);

t_packet UTiut_Wait_for_Master_Tx(t_packetTypes valid_packets, u_int32 max_frames);
t_packet UTiut_Wait_for_Master_Tx_eSCO(t_packetTypes valid_packets, u_int32 max_frames);
void UTiut_Wait_for_Piconet_Clock_Value(u_int32 switch_instant);

t_packet UTiut_Wait_for_Master_Default_Tx(void);
t_packet UTiut_Wait_For_Master_Tx_Connection_Handle(u_int16 connection_handle);

void UTiut_Process_Multiple_Frames(u_int32 num_frames);
void UTiut_Process_Multiple_Frames_No_Tx(u_int32 num_frames);
void UTiut_Process_Multiple_Frames_Verify_Tx_Rx(u_int32 num_frames, 
     t_packet expected_tx_pkt, t_packet expected_rx_packet);

void UTiut_Tx_Poll_Rx_Null(u_int32 num_of_frames);
void UTiut_Tx_Poll_Rx_Null_After_Switch(u_int32 num_of_frames);
void UTiut_Rx_Poll_Tx_Null(u_int32 num_of_frames);

/*****************************************************************************
 * UTiut_Process_Tx_Slot
 *
 * Process the next Transmit Slot
 *****************************************************************************/
#define UTiut_Process_Tx_Slot()         Testsup_Process_Tx_Slot()

/*****************************************************************************
 * UTiut_Process_Rx_Slot
 *
 * Process the next Receive Slot
 *****************************************************************************/
#define UTiut_Process_Rx_Slot()         Testsup_Process_Rx_Slot()

/*****************************************************************************
 * UTiut_Increment_Clocks
 *
 * Increment the Native and BT clocks by ticks
 *****************************************************************************/
#define BT_CLOCK_TICKS_PER_SECOND   3200
#define UTiut_Increment_Clocks(ticks)   Testsup_Increment_Clocks(ticks)

#define UTiut_TAB_INTRASLOT_OFFSET 625
#define UTiut_LMP_INTRASLOT_OFFSET ((UTiut_TAB_INTRASLOT_OFFSET+625)%1250)

/*****************************************************************************
 * UTiut_Is_Receiver_Enabled
 *
 * Indicates if the receiver can receive a packet or not.
 *****************************************************************************/
#define UTiut_Is_Receiver_Enabled()     (HW_get_rx_mode()!=0)

/*************************************************************************
 * UTiut_Receive_Packet_In_Baseband
 *
 * Place the presented packet in the Baseband registers if receiver enabled.
 *
 * Returns     packet type requested if successful (receiver enabled)
 *             INVALIDpkt if receiver not enabled.
 *************************************************************************/
#define UTiut_Receive_Packet_In_Baseband(packet_type, am_addr, seqn, arqn, flow, \
        length, payload, l_ch, p_flow, crc, hec) \
        Testsup_Receive_Packet(packet_type, am_addr, seqn, arqn, flow, \
        length, payload, l_ch, p_flow, crc, hec)

#define UTiut_Verify_Tx_Header_In_Baseband(pkt, arqn, flow, seqn) \
        Testsup_Verify_Tx_Packet(pkt, arqn, flow, seqn)
#define UTiut_Verify_Rx_Header_In_Baseband(pkt, arqn, flow, seqn) \
        Testsup_Verify_Rx_Packet(pkt, arqn, flow, seqn)
/*
 * Persistent selection for 1st/2nd half ID and FHS receives
 */
#define UTiut_Force_1st_Half_Slot_ID_Rx()  \
        TESTsup_Set_ID_FHS_Rx_Position(FIRST_HALF_SLOT)
#define UTiut_Force_2nd_Half_Slot_ID_Rx()  \
        TESTsup_Set_ID_FHS_Rx_Position(SECOND_HALF_SLOT)
#define UTiut_Force_1st_Half_Slot_FHS_Rx()  \
        TESTsup_Set_ID_FHS_Rx_Position(FIRST_HALF_SLOT)
#define UTiut_Force_2nd_Half_Slot_FHS_Rx()  \
        TESTsup_Set_ID_FHS_Rx_Position(SECOND_HALF_SLOT)

#define UTiut_NULL_CONNECTION_HANDLE 0xFFFF
/*****************************************************************************
 * UTiut_Get_Handle_For_Bd_Addr
 *
 * Returns the ACL Connection Handle for the bd_addr if it exists
 *         otherwise returns UTiut_NULL_CONNECTION_HANDLE
 *****************************************************************************/
t_connectionHandle UTiut_Get_Handle_For_Bd_Addr(const u_int8 *bd_addr_bytes);

/*****************************************************************************
 * UTiut_Get_ACL_Handle
 *
 * Returns the ACL Connection Handle of Active Selected Tester 
 *         otherwise returns UTiut_NULL_CONNECTION_HANDLE
 *****************************************************************************/
const t_connectionHandle UTiut_Get_ACL_Handle(void);

/*****************************************************************************
 *
 * UTiut_Get_ACL_Handle_For_SYN_Handle
 *
 * Returns the ACL Connection Handle associated with a specified
 * SYN handle
 *
 *****************************************************************************/
const t_connectionHandle UTiut_Get_ACL_Handle_For_SYN_Handle(const t_connectionHandle syn_handle);

/*****************************************************************************
 * UTiut_Get_SCO_Handle
 *
 * Returns the SCO Connection Handle of selected SCO
 *****************************************************************************/
const t_connectionHandle UTiut_Get_SCO_Handle(u_int8 sco_ref);

/*****************************************************************************
 *  UTiut_Verify_SCO_Hardware
 * 
 *  Verify that the hardware is correctly setup for this SCO.
 *  Returns TRUE if all ok, FALSE if error
 *****************************************************************************/
boolean UTiut_Verify_SCO_Hardware(u_int8 sco_connection_number);

/*****************************************************************************
 * UTiut_Get_Handle_For_Last_Master_Active_Slot
 *
 * Returns the ACL Connection Handle of last Active Lower Tester if active
 *       otherwise returns  UTiut_NULL_CONNECTION_HANDLE 
 * This corresponds to handle of device for which last BB packet was
 * sent/received (based on am_addr).
 *
 * Doesn't support scatternet of 2 slaves with same am_addr
 * 
 *****************************************************************************/
const t_connectionHandle UTiut_Get_Handle_For_Last_Master_Active_Slot(void);

/*****************************************************************************
 * UTiut_Get_Bd_Addr_Ref
 *
 * Returns reference to the BDADDR of Implementation Under Test
 *****************************************************************************/
const t_bd_addr* UTiut_Get_Bd_Addr_Ref(void);

/*****************************************************************************
 * UTiut_Get_Bd_Addr_Byte_Array_Ref
 *
 * Returns reference to a byte array representation of BDADDR of IUT
 *****************************************************************************/
const u_int8 *UTiut_Get_Bd_Addr_Byte_Array_Ref(void);

/*****************************************************************************
 * UTiut_Get_Device_Role
 *
 * Returns Device Role of Implementation Under Test
 *****************************************************************************/
#define UTiut_Get_Device_Role()         UTgen_Get_IUT_Device_Role()

/*****************************************************************************
 * UTiut_Get_Device_Role
 *
 * Sets Device Role of IUT - needed for MSS
 *****************************************************************************/
#define UTiut_Set_Device_Role(new_role) UTgen_Set_IUT_Device_Role(new_role)

/*****************************************************************************
 * UTiut_Get_Device_Features_Ref
 *
 * Returns a reference to the feature set vector of Implementation under Test
 *****************************************************************************/
u_int8* UTiut_Get_Device_Features_Ref(void);

/*****************************************************************************
 *
 * UTiut_Get_Device_Extended_Features_Ref
 *
 * Returns a reference to the extended feature set vector of IUT pointed
 * to by page
 *
 *****************************************************************************/
u_int8* UTiut_Get_Device_Extended_Features_Ref(u_int8 page);


/*****************************************************************************
 * UTiut_Get_Device_Link_Ref
 *
 * Returns a reference to the internal device link for the IUT
 *****************************************************************************/
t_devicelink* UTiut_Get_Device_Link_Ref(void);


typedef enum {RT_AM_ADDRS, RT_DEVICE_LINKS, RT_LMP_LINKS, RT_HCI_EVENTS, 
                RT_NUM_T_RESOURCE_TYPE} t_resource_type;
/*****************************************************************************
 * UTiut_Allocate_All_Resources
 *
 * Allocates all resources of given type for error checking
 *****************************************************************************/
u_int32 UTiut_Allocate_All_Resources(t_resource_type resource_type);

/*****************************************************************************
 * UTiut_Free_All_Resources
 *
 * Free all resources of given type for error checking
 *****************************************************************************/
u_int32 UTiut_Free_All_Resources(t_resource_type resource_type);

/*
 * Useful general accessors
 */
u_int16 UTiut_Set_Link_Supervision_Timeout(u_int16 handle, u_int16 link_supervision_timeout); 
u_int16 UTiut_Get_Link_Supervision_Timeout(u_int16 handle);
u_int16 UTiut_Set_Poll_Interval(u_int16 poll_interval); 
u_int16 UTiut_Set_Poll_Interval_And_Reset_Poll_Position(u_int16 poll_interval);
u_int16 UTiut_Set_Poll_Interval_For(u_int16 acl_handle, u_int16 poll_interval); 
void UTiut_Reset_Poll_Position_For(u_int16 acl_handle);

u_int16 UTiut_Get_Poll_Interval(void);
void UTiut_Set_Power_Level(u_int8 power_level);
u_int8 UTiut_Get_Power_Level(void);
void UTiut_Set_Radio_RSSI_Level_dBm(s_int8 rssi_dBm);
void UTiut_Set_RSSI_Average(t_devicelink* p_device_link, s_int8 rssi);
u_int16 UTiut_Get_Clock_Offset(void);
u_int8 UTiut_Get_LM_SCO_Air_Mode(void);
u_int8 UTiut_Get_LM_SCO_Air_Mode_From_Voice_Settings(u_int16 voice_settings);
u_int16 UTiut_Get_Default_Voice_Setting(void);

/*
 * Useful internal SYS accessors for verification
 */
#define UTiut_Is_Feature_Supported(feature_id) SYSconfig_Is_Feature_Supported(feature_id)
#define UTiut_Enable_Feature(feature_id)     SYSconfig_Enable_Feature(feature_id)
#define UTiut_Disable_Feature(feature_id)    SYSconfig_Disable_Feature(feature_id)
#define UTiut_Get_DUT_Mode()    BTtst_Get_DUT_Mode()
#define UTiut_Get_Packet_Length(packet_type)  SYSconst_Get_Packet_Length(packet_type)
#define UTiut_Get_Packet_Segmented_Tx_Length(packet_type)  \
   SYSconst_Get_Packet_Segmented_Tx_Length(packet_type)
 
/*
 * Useful internal HC accessors for verification
 */
#if (PRH_BS_CFG_SYS_HC_TO_HOST_FLOW_CONTROL_SUPPORTED==1)
#define UTiut_Read_Host_Controller_To_Host_Flow_Control() \
    HCfc_Read_Host_Controller_To_Host_Flow_Control()
#else
#define UTiut_Read_Host_Controller_To_Host_Flow_Control() 0/*None*/
#endif
/*
 * Useful internal LC accessors for verification
 */
#define UTiut_Get_Drift()           LC_Read_Local_Device_Drift()
#define UTiut_Get_Jitter()          LC_Read_Local_Device_Jitter()
#define UTiut_Get_Piconet_Clock(deviceindex)   TESTsup_Get_Piconet_Clock(deviceindex)
#define UTiut_Get_Native_Clock()    LC_Get_Native_Clock()
#define UTiut_Get_Default_Piconet_Clock() TESTsup_Get_Piconet_Clock(1/* device_index == 1 */)
#define UTiut_Get_Piconet_Index()         \
    ((u_int8) DL_Get_Piconet_Index(UTiut_Get_Device_Link_Ref()) )

u_int8 UTiut_Get_Next_Free_AM_ADDR(void);

/*
 * UTiut_Is_SCO_Over_HCI_Implemented() returns TRUE if fully implemented
 *     PRH_BS_HCI_VOICE_SETTING_FOR_SCO_VIA_HCI is defined in hc_const.h
 */
#include "hc_const.h"
#ifdef PRH_BS_HCI_VOICE_SETTING_FOR_SCO_VIA_HCI
#define UTiut_Is_SCO_VIA_HCI_Fully_Implemented()     (TRUE)
#else
#define UTiut_Is_SCO_VIA_HCI_Fully_Implemented()     (FALSE)
#define HCfc_Get_Next_HC_to_Host_SCO_Descriptor(sco_index)  ((t_q_descr *)0)
#endif
boolean UTiut_Is_SCO_VIA_HCI_Active(u_int8 sco_connection_number);


/*
 * Macro to allow access to data members of link container for handle
 */
#define UTiut_Get_LMP_Link_Container_Parameter(handle, param, res_var) \
{\
    t_lmp_link *p_lmp_link; \
    p_lmp_link = LMaclctr_Find_Handle(handle); \
    assert(p_lmp_link!=0); \
    res_var = p_lmp_link->param; \
}

#define UTiut_Verify_LMP_Link_Container_Parameter(handle, param, value) \
{\
    t_lmp_link *p_lmp_link; \
    p_lmp_link = LMaclctr_Find_Handle(handle); \
    assert(p_lmp_link!=0); \
    if(p_lmp_link->param != value) \
    { \
        pDebugPrint(pLOGERROR, "UTiut_Verify_LMP_Link_Container_Parameter>>> " \
          "Mismatch in expected link container <%s>=%d and not %d\n", \
           #param, p_lmp_link->param, value); \
        UTgen_Force_Test_Status(PRTH_TEST_FAILED); \
    } \
}

#define UTiut_Get_Tx_Seqn()         \
    ((t_seqn) DL_Get_Tx_LT_Seqn(UTiut_Get_Device_Link_Ref()) )

#define UTiut_Set_Tx_Seqn(seqn)         \
    (DL_Set_Tx_LT_Seqn(UTiut_Get_Device_Link_Ref(),(seqn)) )



#define UTiut_Set_Rx_Previous_Arqn(arqn) \
    (DL_Set_Rx_LT_Previous_Arqn(UTiut_Get_Device_Link_Ref(), (arqn)) )


#define UTiut_Get_Rx_Expected_Seqn() \
    ((t_seqn) DL_Get_Rx_LT_Expected_Seqn(UTiut_Get_Device_Link_Ref()) )


/*
 * Useful internal LMP accessors for verification
 */
#define UTiut_Get_Connection_Auto_Accept_Status() \
 LMfltr_Connection_Filter_Check((t_bd_addr*)UTiut_Get_Bd_Addr_Ref(), 0)

/*
 * Useful internal Baseband accessors for verification
 */
boolean UTiut_Is_Hardware_Rx_Buffer(const u_int8 *rx_buffer, u_int32 length);
boolean UTiut_Is_Hardware_Tx_Buffer(const u_int8 *tx_buffer, u_int32 length);

/**************************************************************************
 * UTiut_Enqueue_HCI_Command (should not be used by test functions)
 **************************************************************************/
void UTiut_Tx_HCI_Packet_To_HC(
    u_int8 q_type, const u_int8 *p_pdu, u_int32 length);

void UTiut_Enqueue_HCI_Command(
    const u_int8* command_buf, u_int32 command_len);

void UTiut_Enqueue_HCI_ACL_Data_Packet(u_int16 handle,
      u_int8 pb_flag, u_int8 bc_flag, const u_int8* data_buf, u_int32 data_len);

void UTiut_Enqueue_HCI_ACL_Data_Event(u_int16 handle,
      u_int8 pb_flag, u_int8 bc_flag, const u_int8* data_buf, u_int32 data_len);

/**************************************************************************
 * UTiut_Process_Logged_Events
 * *************************************************************************/
void UTiut_Process_Logged_Events(void);

/**************************************************************************
 * UTiut_Force_HCI_Reset
 *
 * Force a full device reset
 *************************************************************************/
void UTiut_Force_HCI_Reset(void);

/**************************************************************************
 * UTiut_Create_Default_Connection_To_Tester
 *
 * Setup a valid connection to the current tester based on current device role
 *
 * Returns connection_handle
 *************************************************************************/
u_int16 UTiut_Create_Default_Connection_To_Tester(void);

/**************************************************************************
 * UTiut_Create_Default_Connection_To_Tester_Pre_Setup_Complete
 *
 * Setup a valid connection to the current tester based on current device role
 * but do not complete the connection.
 * Effectively, go as far as ack of LMP_host_connection_req()
 *
 * Returns connection_handle
 *************************************************************************/
u_int16 UTiut_Create_Default_Connection_To_Tester_Pre_Setup_Complete(void);

/**************************************************************************
 * UTiut_Disconnect_Connection_To_Tester
 *
 * Disconnect existing connection to the current tester 
 *
 * This version always disconnects via upper tester.
 *************************************************************************/
void UTiut_Disconnect_Connection_To_Tester(void);


u_int16 UTiut_Get_Active_Packets(void);
u_int8 UTiut_Get_Active_Max_Slots_Out(void);   

#define UTiut_Print_Buffer     Testsup_Print_Buffer

/*****************************************************************************
 * UTiut_Satisfy_Initialization_Equations
 *
 * Process Multiple Frames until initialization equations are satisfied. Used
 * by Sniff/Park tests
 *****************************************************************************/
void UTiut_Satisfy_Initialization_Equations(
    u_int16 Tvalue, u_int16 Dvalue, u_int8 timing_control_flags);

#define IUT_COMBINATION_KEY 0
#define IUT_UNIT_KEY        1
#define IUT_AUTH_OFF        0
#define IUT_AUTH_ON         1
#define IUT_ENCR_OFF        0
#define IUT_ENCR_ON         1
#define IUT_VARIABLE_PIN    0
#define IUT_FIXED_PIN       1
/*****************************************************************************
 *  Function: UTiut_Set_Device_Security_Settings()
 *
 *  Purpose:  Set the device security settings
 *
 *  iut_key_type    IUT_COMBINATION_KEY (0),  IUT_UNIT_KEY (1)
 *  auth_enb        IUT_AUTH_OFF (0), IUT_AUTH_ON (1)
 *  enc_mode        IUT_ENCR_OFF (0), IUT_ENCR_ON (1)
 *  pin_type        IUT_VARIABLE_PIN (0), IUT_FIXED_PIN (1)
 *****************************************************************************/
void UTiut_Set_Device_Security_Settings(
    u_int8 iut_key_type, u_int8 auth_enb, u_int8 enc_mode, u_int8 pin_type);


/*****************************************************************************
 * UTiut_Get_Encryption_Key_Size_Mask
 *
 * Purpose: Get the devices encryption key size mask.
 * 
 *****************************************************************************/
u_int16 UTiut_Get_Encryption_Key_Size_Mask(void);

/*****************************************************************************
 * UTiut_Modify_Poll_Interval
 *
 * Change the Poll Interval to New_Poll_Interval
 *****************************************************************************/
void UTiut_Modify_Poll_Interval(u_int16 New_Poll_Interval);

/*****************************************************************************
 * UTiut_Handle_Feature_If_Supported
 *
 * Process the functionality associated with the requested feature
 *****************************************************************************/
void UTiut_Handle_Feature_If_Supported(u_int8 role, u_int16 feature);

/*****************************************************************************
 * UTiut_Handle_All_Features_On_Connection_Setup
 *
 * Process the features which occur on connection setup or switch.  
 *****************************************************************************/
void UTiut_Handle_All_Features_On_Connection_Setup(u_int8 role);

/*****************************************************************************
 * UTiut_Assert_Frequency_is_Piconet()
 *
 * piconet_clock               Master Tx/Rx Clock for next packet
 *
 * Check that the Rx frequency matches the Master Tx for given BT clock
 *****************************************************************************/
void UTiut_Assert_Frequency_is_Piconet(t_clock piconet_clock);
/*****************************************************************************
 * UTiut_Determine_Frequency_79
 *
 * Calculate the frequency for given uap_lap and state
 *****************************************************************************/
u_int8 UTiut_Determine_Frequency_79(
   t_uap_lap uap_lap, t_clock clk, t_state state, u_int X);
u_int8* UTiut_Build_Frequency_Table_79(t_state state, t_uap_lap uap_lap);
u_int8 UTiut_Get_Frequency_Table_Index(u_int8 *freq_table, u_int8 size, u_int8 freq);
u_int8 UTiut_Get_Frequency_Channel(u_int8 freq_pos_required);
u_int8 UTiut_Get_Rx_Frequency(void);
u_int8 UTiut_Get_Tx_Frequency(void);

u_int16 UTiut_Get_Raw_Rssi(void);
void UTiut_Set_Raw_Rssi(u_int16 raw_rssi);

/*
 * Definitions to support backwards compatability.
 */



/*
 * Patch 3 SCO B1667 has been applied
 * Target for correct naming to avoid confusion with LMP
 */
#define ILLEGAL_PARAMETER INVALID_HCI_PARAMETERS

#ifndef PRH_BS_CFG_SYS_LMP_AFH_CAPABLE_MASTER_SUPPORTED
#define PRH_BS_CFG_SYS_LMP_AFH_CAPABLE_MASTER_FEATURE       0x0508
#define PRH_BS_CFG_SYS_LMP_AFH_CAPABLE_MASTER_SUPPORTED     0
#endif

#ifndef PRH_BS_CFG_SYS_LMP_AFH_CAPABLE_SLAVE_SUPPORTED
#define PRH_BS_CFG_SYS_LMP_AFH_CAPABLE_SLAVE_FEATURE       0x0408
#define PRH_BS_CFG_SYS_LMP_AFH_CAPABLE_SLAVE_SUPPORTED     0
#endif

#endif
